package com.linhh.mall.center.web.controller;

import com.linhh.mall.center.api.TbLandAttachmentService;
import com.linhh.mall.center.api.TbLandCoordinateService;
import com.linhh.mall.center.api.TbLandRegistrationService;
import com.linhh.mall.center.common.pojo.TbLandAttachment;
import com.linhh.mall.center.common.pojo.TbLandCoordinate;
import com.linhh.mall.center.common.pojo.TbLandRegistration;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author linhh
 * @Date 2023 03 20 14 11
 **/
@RestController
@RequestMapping("/api/Registration")
public class TbLandRegistrationController {

    @Reference
    private TbLandRegistrationService tbLandRegistrationService;

    @Reference
    private TbLandCoordinateService tbLandCoordinateService;
    @Reference
    private TbLandAttachmentService tbLandAttachmentService;

    @GetMapping("/all")
    public List<TbLandRegistration> getLandRegistrations(){
        List<TbLandRegistration> tbLandRegistrations = tbLandRegistrationService.selectAll();

        List<TbLandCoordinate> tbLandCoordinates = new ArrayList<>();
        List<TbLandAttachment> tbLandAttachments = new ArrayList<>();

        for (TbLandRegistration tbLandRegistration:tbLandRegistrations){
           TbLandCoordinate tbLandCoordinate = tbLandCoordinateService.selectCoordinateByLandId(tbLandRegistration.getId());
            TbLandAttachment tbLandAttachment = tbLandAttachmentService.selectAttachmentByLandId(tbLandRegistration.getId());
            if(tbLandCoordinate!=null){
                tbLandCoordinates.add(tbLandCoordinate);
            }
            if(tbLandAttachment!=null){
                tbLandAttachments.add(tbLandAttachment);
            }
            if(tbLandRegistration.getId().equals(tbLandCoordinate.getLandId())){
                tbLandRegistration.setCoordinates(tbLandCoordinates);
            }
            if(tbLandRegistration.getId().equals(tbLandAttachment.getLandId())){
                tbLandRegistration.setAttachments(tbLandAttachments);
            }

            tbLandRegistrations.add(tbLandRegistration);
        }
        return tbLandRegistrations;
    }

    @GetMapping("/all1")
    public List<TbLandRegistration> getLandRegistrationList(){
        return tbLandRegistrationService.selectAll();
    }


    @DeleteMapping("/delete/{id}")
    public int deleteLandRegistration(@PathVariable Long id){
        return tbLandRegistrationService.deleteTbLandRegistration(id);
    }
    @PostMapping("/add")
    public int addLandRegistration(@RequestBody TbLandRegistration tbLandRegistration){
        return tbLandRegistrationService.addTbLandRegistration(tbLandRegistration);
    }

    @PutMapping("/update")
    public int updateLandRegistration(@RequestBody TbLandRegistration tbLandRegistration){
        return tbLandRegistrationService.updateTbLandRegistration(tbLandRegistration);
    }

    @GetMapping("/{id}")
    public TbLandRegistration getRegistrationById(@PathVariable Long id){
        return tbLandRegistrationService.selectRegistrationById(id);
    }





}
